package com.itheima.reggie.filter.exception;

import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class Exception {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R doSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException exception) {
        log.info("用户操作异常");
        String msg = exception.getMessage();
        if ("Duplicate entry".equals(exception.getMessage())) {
            msg = "用户名已存在";
        }
        return R.error(msg);
    }

    @ExceptionHandler(CustomException.class)
    public R<String> exceptionhandler(CustomException ex) {
        log.error(ex.getMessage());
        return R.error(ex.getMessage());
    }

}
